﻿using System;
using System.Collections.Generic;
using Drill.Configuration;

namespace Drill.Core
{
	public class ConfigBasedSearchPathFactory : ISearchPathFactory
	{
		private readonly ISearchPathConfigurationCollection _searchPathConfigs;

		public ConfigBasedSearchPathFactory(ISearchPathConfigurationCollection searchPathConfigs)
		{
			if (null == searchPathConfigs)
			{
				throw new ArgumentNullException("searchPathConfigs");
			}
			if (searchPathConfigs.Count < 1)
			{
				throw new ArgumentException("The searchPathConfigs collection is empty but it must contain at least one item.", "searchPathConfigs");
			}
			_searchPathConfigs = searchPathConfigs;
		}

		public IList<ISearchPath> CreateSearchPaths()
		{
			List<ISearchPath> searchPaths = new List<ISearchPath>(_searchPathConfigs.Count);
			foreach (var searchPathConfig in _searchPathConfigs)
			{
				if (searchPathConfig.Enabled)
				{
					string path = searchPathConfig.Path;
					if (false == string.IsNullOrWhiteSpace(path))
					{
						searchPaths.Add(new SearchPath { Value = path });
					}
				}
			}
			return searchPaths;
		}
	}
}